package com.wbdgj.ui.store;

import android.content.Context;
import android.content.Intent;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.adapter.CompaignMoreAdapter;
import com.wbdgj.adapter.MemberPointAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import retrofit2.Call;

public class PointsDetailsActivity extends BaseActivity {

    private Context context = this;
    @BindView(R.id.kyjf)
    TextView kyjf;

    MemberPointAdapter memberPointAdapter;
    @BindView(R.id.demo_listview)
    PullListView demo_listview;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;

    private int page = 1;
    private int total = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_points_details;
    }

    @Override
    protected void initView() {
        getMyIntegralInfoByApp();
        queryIntegralByApp();

        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        queryIntegralByApp();
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if((page*10>=total)){
                    ToastUtils.toastShort("已无更多");
                }else{
                    page ++ ;
                    queryIntegralByApp();
                }
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });
    }

    private void getMyIntegralInfoByApp() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMyIntegralInfoByApp(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID),BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        LinkedTreeMap dateLink = (LinkedTreeMap) linkedTreeMap.get("data");
                        if(dateLink.containsKey("AvailableIntegral")){
                            kyjf.setText(decimalFormat.format(dateLink.get("AvailableIntegral")));
                        }
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    private void queryIntegralByApp() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().queryIntegralByApp(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.HOTEL_ID),BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN), page+"", "10");
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if(linkedTreeMap.containsKey("data")){
                        ArrayList<LinkedTreeMap> arrayList = (ArrayList<LinkedTreeMap>) linkedTreeMap.get("data");
                        if(page == 1){
                            memberPointAdapter = new MemberPointAdapter(context, arrayList);
                            demo_listview.setAdapter(memberPointAdapter);
                        }else{
                            memberPointAdapter.loadMoreListView(arrayList);
                            memberPointAdapter.notifyDataSetChanged();
                        }

                    }

                    if(linkedTreeMap.containsKey("Pager")){
                        LinkedTreeMap pagerLink = (LinkedTreeMap) linkedTreeMap.get("Pager");
                        if(pagerLink.containsKey("total")){
                            total = Integer.parseInt(decimalFormat.format(pagerLink.get("total")));
                        }
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }
}
